package com.tcp2.util;

/**
 * Author: <黄书根>
 * Time: 2017-08-02 15:43
 * Copyright (C) 2017 Xiamen Yaxon Networks CO.,LTD.
 */
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.net.URLDecoder;
import java.util.Properties;

public class ConfigUtil {

    static String webPath;
    public static String getWebRootPath() {
        if(webPath!=null){
            return webPath;
        }
        String path = URLDecoder.decode(ConfigUtil.class
                .getResource("/").getPath());
        File file = new File(path);
        path = file.getAbsolutePath();
        path = path.replace("\\", "/");
        path = path.substring(0, path.lastIndexOf("/"));
        path = path.substring(0, path.lastIndexOf("/"));
        path += "/";
        webPath=path;
        return webPath;
    }

    public static String getFileIO(String name,String fileURL){
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String path    = cl.getResource("").getPath();
        String filePath = path;
        String p = filePath+ fileURL;
        Properties prop = new Properties();
        InputStream is = null;
        try {
            Resource resource = new ClassPathResource(fileURL);
            InputStream is1 = resource.getInputStream();
            is = new FileInputStream(p);
            BufferedReader bf = new BufferedReader(new  InputStreamReader(is1, "UTF-8"));//  确保读取的文件编码是 utf-8  否则则中文会出现乱码
            prop.load(bf);

         // String   newValue = new String(prop.getProperty(name).getBytes("ISO-8859-1"),"UTF-8");
           // System.out.println(prop.getProperty(name));
            return prop.getProperty(name);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
    public static void writeData(String key, String value,String fileURL) {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String path    = cl.getResource("").getPath();
        String filePath = path;
        String p = filePath+ fileURL;
        Properties prop = new Properties();
        InputStream is = null;
        OutputStream fos = null;
        try {

            is = new FileInputStream(p);
            BufferedReader bf = new BufferedReader(new  InputStreamReader(is, "UTF-8"));//  确保读取的文件编码是 utf-8  否则则中文会出现乱码
            prop.load(bf);
            is.close();//一定要在修改值之前关闭fis
            fos = new FileOutputStream(p);
            prop.setProperty(key, value);
            prop.store(fos, "");
            //  prop.store(fos, "Update '" + key + "' value");
            fos.close();

        } catch (IOException e) {
            System.err.println("Visit " + fileURL + " for updating "
                    + value + " value error");
        }
        finally{
            try {
                fos.close();
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
